#include <QScrollArea>
#include <QSlider>

#include "View.hpp"
#include "ui_View.h"


View::View(QWidget *parent) :
    QMainWindow(parent),
    _ui(new Ui::View)
{
    _ui->setupUi(this);

    _model = new Model();
    _model->setObserver(this);

    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setBackgroundRole(QPalette::Midlight);

    _drawingArea = new DrawingArea(this);
    _drawingArea->setModel(_model);

    _ui->actionDelete->setShortcut(QKeySequence::Delete);

    scrollArea->setWidget(_drawingArea);
    setCentralWidget(scrollArea);
    setWindowTitle("tree mapper");
}

View::~View()
{
    delete _ui;
}


void View::notifyChanges()
{
    repaint();
    _drawingArea->repaint();
}

void View::paintEvent(QPaintEvent* event)
{
    /* Activer/desactiver les boutons ici, en fonction du modele */
    _ui->actionAddSibling->setEnabled(_model->canAddSiblingToSelection());
    _ui->actionAddChild->setEnabled(_model->canAddChildToSelection());
    _ui->actionDelete->setEnabled(_model->isSelectionDeletable());
    _ui->actionAttributes->setEnabled(_model->canChangeSelectionSettings());
    _ui->newNameEdit->setEnabled(_model->canChangeSelectionSettings());

    _ui->abstractButton->setEnabled(_model->canChangeSelectionSettings());
    _ui->userButton->setEnabled(_model->canChangeSelectionSettings());
    _ui->applicationButton->setEnabled(_model->canChangeSelectionSettings());
    _ui->interactionButton->setEnabled(_model->canChangeSelectionSettings());
    _ui->iterativeButton->setEnabled(_model->getSelection());
    _ui->optionalButton->setEnabled(_model->getSelection());

    bool canChangeRelation = _model->canChangeSelectionRelationType();
    _ui->choiceButton->setEnabled(canChangeRelation);
    _ui->orderIndependanceButton->setEnabled(canChangeRelation);
    _ui->interleavingButton->setEnabled(canChangeRelation);
    _ui->synchronizationButton->setEnabled(canChangeRelation);
    _ui->disablingButton->setEnabled(canChangeRelation);
    _ui->suspendResumeButton->setEnabled(canChangeRelation);
    _ui->sequenceButton->setEnabled(canChangeRelation);
    _ui->sequentialEnablingInfoButton->setEnabled(canChangeRelation);

    _ui->actionUndo->setEnabled(_model->canUndo());
    _ui->actionRedo->setEnabled(_model->canRedo());

    if(_model->getSelection())
    {
       if(_ui->newNameEdit->text() != _model->getSelection()->getName().c_str())
           _ui->newNameEdit->setText(_model->getSelection()->getName().c_str());
    }
    else
        _ui->newNameEdit->setText("");

    /* Appel de la methode de la classe mere */
    QMainWindow::paintEvent(event);

}

void View::on_actionExit_triggered()
{
    QApplication::exit();
}


void View::on_ZoomSlider_valueChanged(int value)
{
    _ui->lcdZoom->display(value);
    _drawingArea->setZoom((double)value / 100);
    _drawingArea->repaint();
}

void View::on_actionAttributes_triggered()
{
    if(_model->getSelection())
    {
        AttributesDialog attributesDialog;
        attributesDialog.setNodeInfo(_model->getSelection());
        if(attributesDialog.exec())
        {
            _model->changeSelectionSettings(attributesDialog.getName().toStdString(),attributesDialog.getType());
            notifyChanges();
        }
    }
}

void View::on_abstractButton_clicked()
{
    if(_model->getSelection())
        _model->changeSelectionSettings(_model->getSelection()->getName(),Node::ABSTRACT);
}

void View::on_userButton_clicked()
{
    if(_model->getSelection())
        _model->changeSelectionSettings(_model->getSelection()->getName(),Node::USER);
}

void View::on_applicationButton_clicked()
{
    if(_model->getSelection())
        _model->changeSelectionSettings(_model->getSelection()->getName(),Node::APPLICATION);
}

void View::on_interactionButton_clicked()
{
    if(_model->getSelection())
        _model->changeSelectionSettings(_model->getSelection()->getName(),Node::INTERACTION);
}

void View::on_newNameEdit_textChanged(QString )
{
    if(_model->getSelection())
    {
        if(_ui->newNameEdit->text().toStdString() != _model->getSelection()->getName())
        _model->changeSelectionSettings(_ui->newNameEdit->text().toStdString(),
                                        _model->getSelection()->getType());
    }
}

void View::on_redrawAction_triggered()
{
    _model->redrawTree();
}

void View::on_actionAddChild_triggered()
{
    _model->addChildToSelection();
}

void View::on_actionAddSibling_triggered()
{
    _model->addSiblingToSelection();
}

void View::on_iterativeButton_clicked()
{
    if(_model->getSelection())
        _model->manageIterative();
    _drawingArea->repaint();
}

void View::on_optionalButton_clicked()
{
    if(_model->getSelection())
        _model->manageOptional();
    _drawingArea->repaint();
}

void View::on_sequenceButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::SEQUENCE);
    _drawingArea->repaint();
}

void View::on_choiceButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::CHOICE);
    _drawingArea->repaint();
}

void View::on_orderIndependanceButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::ORDER_INDEPENDANCE);
    _drawingArea->repaint();
}

void View::on_interleavingButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::INTERLEAVING);
    _drawingArea->repaint();
}

void View::on_synchronizationButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::SYNCHRONIZATION);
    _drawingArea->repaint();
}

void View::on_disablingButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::DISABLING);
    _drawingArea->repaint();
}

void View::on_suspendResumeButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::SUSPEND_RESUME);
    _drawingArea->repaint();
}

void View::on_sequentialEnablingInfoButton_clicked()
{
    if(_model->canChangeSelectionRelationType())
        _model->setSelectionRelationType(Node::SEQUENTIAL_ENABLING_INFO);
    _drawingArea->repaint();
}

void View::on_actionDelete_triggered()
{
    if(_model->isSelectionDeletable())
        _model->deleteSelection();
}
